module.exports = (app, express) => {
  const Hero = require('../../model/Hero.js')
  const router = express.Router()
  router.get('/', (req, res) => {
    res.send('success')
  })

  router.post('/heros', async (req, res) => {
    try {
      req.body.add_time = new Date()
      await Hero.create(req.body)
      res.send({
        status: 0,
        success: true
      })
    } catch (error) {
      res.send({
        status: 1,
        message: '接口请求失败'
      })
    }
  })

  router.get('/heros', async (req, res) => {
    try {
      const heros = await Hero.find()
      res.send({
        status: 0,
        message: heros
      })
    } catch (error) {
      res.send({
        status: 1,
        message: '接口请求失败'
      })
    }
  })

  router.get('/heros/:id', async (req, res) => {
    try {
      const heros = await Hero.findById(req.params.id)
      res.send({
        status: 0,
        message: heros
      })
    } catch (error) {
      res.send({
        status: 1,
        message: '接口请求失败'
      })
    }
  })

  router.put('/heros/:id', async (req, res) => {
    try {
      await Hero.findByIdAndUpdate(req.params.id, req.body)
      res.send({
        status: 0,
        success: true
      })
    } catch (error) {
      res.send({
        status: 1,
        message: '接口请求失败'
      })
    }
  })

  router.delete('/heros/:id', async (req, res) => {
    try {
      await Hero.findByIdAndDelete(req.params.id)
      res.send({
        status: 0,
        success: true
      })
    } catch (error) {
      res.send({
        status: 1,
        message: '接口请求失败'
      })
    }
  })
  app.use('/api/admin', router)
  var multer = require('multer')
  var path = require('path')
  // 图片放在某个位置
  var upload = multer({ dest: path.join(__dirname, '../../../uploads') })
  app.use('/api/admin/upload', upload.single('file'), (req, res) => {
    // 拼接完成的图片路径
    req.url = 'http://127.0.0.1:3000/uploads/' + req.file.filename
    res.send({
      status: 0,
      url: req.url
    })
  })
}